/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
#include "USART_Configuration.h"
#include <stdio.h>
#include "arm_math.h"
#include "IAP.h"
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <stdint.h>
// bool rx1_flag = 0;
void USART1_Begin(uint32_t baudrate)
{
	USART_InitType USART1_InitStructure;
	USART1_InitStructure.USART_BaudRate = baudrate;
	USART1_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART1_InitStructure.USART_StopBits = USART_StopBits_1;
	USART1_InitStructure.USART_Parity = USART_Parity_No;
	USART1_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART1_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_Init(USART1, &USART1_InitStructure);
	USART_INTConfig(USART1, USART_INT_RDNE, ENABLE);
	USART_Cmd(USART1, ENABLE);
}
void usart_sendByte(uint8_t b)
{
	// USART_SendData(USART1, b);
	USART1->DT = (b & (uint16_t)0x01FF);
	while (USART_GetFlagStatus(USART1, USART_FLAG_TRAC) != SET)
		;
	// USART_ClearFlag(USART1, USART_FLAG_TRAC);
	USART1->STS = (uint16_t)~USART_FLAG_TRAC;
}
void usart_sendBytes(uint8_t *bs)
{
	while (*bs)
	{
		usart_sendByte(*bs++);
	}
}
uint16_t usart_sendNbytes(uint8_t *bytes, uint16_t len)
{
	while (len--)
	{
		usart_sendByte(*bytes++);
	}
	return len;
}
//redefine printf to usart1
int _write(int fd, char *ptr, int len)
{
	uint16_t count = 0;
	count = len;
	usart_sendNbytes((uint8_t *)ptr, len);
	return count;
}
void USART1_pintf_float(float f, u8 nfs) //nfs: number behind point,max:4
{
	u32 d = 1;
	if (nfs > 4)
		nfs = 4;
	for (u8 i = 0; i < nfs; i++)
	{
		d = d * 10;
	}
	if (nfs == 0)
		d = 1;
	printf("%d.%d", (int16_t)f, (int16_t)(f * d - (int16_t)f * d));
}
char *myitoa(int num, char *str, int radix)
{ /*������*/
	char index[] = "0123456789ABCDEF";
	unsigned unum; /*�м����?*/
	int i = 0, j, k;
	/*ȷ��unum��ֵ*/
	if (radix == 10 && num < 0) /*ʮ���Ƹ���*/
	{
		unum = (unsigned)-num;
		str[i++] = '-';
	}
	else
		unum = (unsigned)num; /*�������?*/
	/*ת��*/
	do
	{
		str[i++] = index[unum % (unsigned)radix];
		unum /= radix;
	} while (unum);
	str[i] = '\0';
	/*����*/
	if (str[0] == '-')
		k = 1; /*ʮ���Ƹ���*/
	else
		k = 0;

	for (j = k; j <= (i - 1) / 2; j++)
	{
		char temp;
		temp = str[j];
		str[j] = str[i - 1 + k - j];
		str[i - 1 + k - j] = temp;
	}
	return str;
}
void rt_kprintf(const char *format, ...)
{
	char str[30];
	char c = 0;
	char ch = 0;
	va_list ap;
	va_start(ap, format);
	while ((c = *format))
	{
		switch (c)
		{
		case '%':
		{
			ch = *++format;

			switch (ch)
			{
			// case 'b':
			// {
			// 	uint32_t n = va_arg(ap, int);
			// 	myitoa(n, str, 2);
			// 	usart_sendBytes(str);
			// 	break;
			// }
			// case 'c':
			// {
			// 	char c = va_arg(ap, int);
			// 	usart_sendByte(c);
			// 	break;
			// }
			case 'd':
			{
				uint32_t n = va_arg(ap, int);
				myitoa(n, str, 10);
				usart_sendBytes((uint8_t*)str);
				break;
			}
			// case 'x':
			// {
			// 	uint32_t n = va_arg(ap, int);
			// 	myitoa(n, str, 16);
			// 	usart_sendBytes(str);
			// 	break;
			// }
			// case 'f':
			// {
			// 	double f = va_arg(ap, double);
			// 	int32_t n;
			// 	n = f;
			// 	myitoa(n, str, 10);
			// 	usart_sendBytes(str);
			// 	usart_sendByte('.');
			// 	n = (f - n) * 1000;
			// 	if (n < 0)
			// 		n = (unsigned)-n;
			// 	myitoa(n, str, 10);
			// 	usart_sendBytes(str);
			// 	break;
			// }
			// case 's':
			// {
			// 	char *p = va_arg(ap, char *);
			// 	usart_sendBytes(p);
			// 	break;
			// }
			// case '%':
			// {
			// 	usart_sendByte('%');
			// 	break;
			// }
			default:
			{
				/*usart_sendBytes( "#error:format invalid: <%");*/
				usart_sendByte('%');
				usart_sendByte(ch);
				/*usart_sendBytes( ">#");*/

				break;
			}
			}
		}
		break;
		default:
			usart_sendByte(c);
			break;
		}
		format++;
	}
	va_end(ap);
	// Serial_flush(&serialHandle[serial1]);
}
void USART1_IRQHandler(void)
{
	// u8 temp;
	if (USART_GetFlagStatus(USART1, USART_FLAG_RDNE) != RESET)
	{
		// USART_ClearFlag(USART1, USART_FLAG_RDNE);
		USART1->STS = (uint16_t)~USART_FLAG_RDNE;
		// temp = USART_ReceiveData(USART1);
		// rx1_flag = ~rx1_flag;
		IAP_USART_Handle(USART1->DT);
	}
}
